/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Utilities;

import Beans.Requests.RequestBean;
import Beans.Responses.ResponseBean;
import Utilities.Constants.SendingRequestResult;
import Utilities.ParameterNames.Service;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class contains functions used by the user interface to manage the sending
 * of request and retrieval of responses.
 *
 * @author Angel Sanadinov
 */
public class UIFunctions
{
    /**
     * Attempts to send the specified request object to the specified service.
     *
     * @param httpRequest HTTP servlet request object associated with the current request
     * @param httpResponse HTTP servlet response object associated with the current request
     * @param serviceRequest the request object
     * @param service the service which will handle the request
     *
     * @return the result of the operation
     *
     * @throws IOException if an I/O error occurs
     * @throws ServletException if the service throws an exception
     *
     * @see SendingRequestResult
     */
    public static SendingRequestResult
            sendServiceRequest(HttpServletRequest httpRequest,
                               HttpServletResponse httpResponse,
                               RequestBean serviceRequest,
                               Service service)
            throws IOException, ServletException
    {
        //checks if the HTTP objects have sane values
        if(httpRequest == null || httpResponse == null)
            return SendingRequestResult.INVALID_HTTP_DATA;
        else
            ;

        //checks if the request object is valid
        if(serviceRequest == null || !serviceRequest.isValid())
            return SendingRequestResult.INVALID_REQUEST;
        else
            ;

        //checks if the specified service has a sane value
        if(service == null || service == Service.INVALID_SERVICE)
            return SendingRequestResult.INVALID_SERVICE;

        RequestDispatcher dispatcher = null;

        //retrieves the proper service dispatcher and sets the proper request attribute
        switch(service)
        {
            case LOGIN_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/LoginService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_LOGIN, serviceRequest);
                else
                    ;
            } break;

            case LOGOUT_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/LoginService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_LOGOUT, serviceRequest);
                else
                    ;
            } break;

            case DATA_RETRIEVAL:
            {
                dispatcher = httpRequest.getRequestDispatcher("/DataService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_DATA_RETRIEVAL, serviceRequest);
                else
                    ;
            } break;

            case USERS_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/UsersManagementService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_MANAGE_USER, serviceRequest);
                else
                    ;
            } break;

            case SERVERS_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/ServersManagementService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_MANAGE_SERVER, serviceRequest);
                else
                    ;
            } break;

            case MACHINES_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/MachinesManagementService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_MANAGE_MACHINE, serviceRequest);
                else
                    ;
            } break;

            case MEDIA_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/MediaManagementService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_MANAGE_MEDIUM, serviceRequest);
                else
                    ;
            } break;

            case NETWORKS_MANAGEMENT:
            {
                dispatcher = httpRequest.getRequestDispatcher("/NetworksManagementService");

                if(dispatcher != null)
                    httpRequest.setAttribute(ParameterNames.REQUEST_MANAGE_NETWORK, serviceRequest);
                else
                    ;
            } break;

            default: ;
        }

        //checks if a dispatcher was retrieved
        if(dispatcher != null)
        {
            //sends the request to the service
            dispatcher.include(httpRequest, httpResponse);
            return SendingRequestResult.OK;
        }
        else
            return SendingRequestResult.INVALID_DISPATCHER;
    }

    /**
     * Attempts to retrieve a response from the specified service.
     *
     * @param httpRequest HTTP servlet request object associated with the current request
     * @param service the service which generated the response
     * @return
     */
    public static ResponseBean retrieveServiceResponse(HttpServletRequest httpRequest, Service service)
    {
        ResponseBean result = null;

        //checks that the supplied input data is valid
        if(httpRequest != null && service != null && service != Service.INVALID_SERVICE)
        {
            //attempts to retrieve the response, based on the specified service
            switch(service)
            {
                case DATA_RETRIEVAL:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_DATA_RETRIEVAL);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_DATA_RETRIEVAL);
                } break;

                case USERS_MANAGEMENT:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_MANAGE_USER);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_MANAGE_USER);
                } break;

                case SERVERS_MANAGEMENT:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_MANAGE_SERVER);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_MANAGE_SERVER);
                } break;

                case MACHINES_MANAGEMENT:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_MANAGE_MACHINE);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_MANAGE_MACHINE);
                } break;

                case MEDIA_MANAGEMENT:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_MANAGE_MEDIUM);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_MANAGE_MEDIUM);
                } break;

                case NETWORKS_MANAGEMENT:
                {
                    result = (ResponseBean)httpRequest.getAttribute(ParameterNames.RESPONSE_MANAGE_NETWORK);
                    httpRequest.removeAttribute(ParameterNames.RESPONSE_MANAGE_NETWORK);
                } break;

                default: ;
            }
        }
        else
            ;

        return result;
    }

    /**
     * Parses the specified result and returns the appropriate exception code.
     *
     * @param result the result of a sending operation
     * @return the exception code for the specified result
     *
     * @see ExceptionMessages
     */
    public static int parseSendingRequestResult(SendingRequestResult result)
    {
        int exceptionCode = ExceptionMessages.APP_E_UNKNOWN_EXCEPTION;
        switch(result)
        {
            case INVALID_HTTP_DATA:
                exceptionCode = ExceptionMessages.APP_E_REQUEST_SEND_FAILED_INVALID_HTTP_DATA; break;

            case INVALID_REQUEST:
                exceptionCode = ExceptionMessages.APP_E_REQUEST_SEND_FAILED_INVALID_REQUEST; break;

            case INVALID_SERVICE:
                exceptionCode = ExceptionMessages.APP_E_REQUEST_SEND_FAILED_INVALID_SERVICE; break;

            case INVALID_DISPATCHER:
                exceptionCode = ExceptionMessages.APP_E_REQUEST_SEND_FAILED_INVALID_DISPATCHER; break;

            default: ; break;
        }

        return exceptionCode;
    }
}
